/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package WordTable;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import GuiTools.CustomScrollPane;
import GuiTools.CustomTextArea;
import GuiTools.LabelPanel;
import Settings.Language;
import Storage.Meaning;
import Storage.TermClass;

/**
 * @author Christoph Schnack
 *
 */
class MeaningDisplayPanel extends JPanel{
	private TermClass termClass;
	private JPanel meaningsPanel;
	private GridBagConstraints gbc,wlgbc, mgbc;
	private int rowCounter = 0;
	private Font phoneticFont = new Font(Font.SERIF,Font.BOLD,15);
	private Insets noInsets = new Insets(0,0,0,0),meaningInsets=new Insets(5,20,0,20),spaceLeftFiveInsets=new Insets(0,5,0,0),spaceLeftTenInsets=new Insets(0,10,0,0);
	private Font valFont = new Font(Font.SANS_SERIF,Font.BOLD,12),keyFont = new Font(Font.SANS_SERIF,Font.PLAIN,12),meaningFont = new Font(Font.SANS_SERIF,Font.BOLD,25);
	private Language LANG;
	private WCSettings wcSettings;
	
	public MeaningDisplayPanel(Language lang, WCSettings wcSettingsArg, Vector<Meaning> mngs, TermClass tc, String tcName, String section){
		super();
		LANG = lang;
		wcSettings = wcSettingsArg;
		termClass = tc;
		meaningsPanel = new JPanel();
		meaningsPanel.setLayout(new GridBagLayout());
		meaningsPanel.setOpaque(false);
		gbc = new GridBagConstraints();
		wlgbc = new GridBagConstraints();
		wlgbc.fill = GridBagConstraints.BOTH;
		wlgbc.anchor = GridBagConstraints.WEST;
		wlgbc.weightx = 1;
		mgbc = new GridBagConstraints();
		mgbc.anchor = GridBagConstraints.NORTHWEST;
		mgbc.fill = GridBagConstraints.BOTH;
		mgbc.weightx = 1;

		setUpMeanings(mngs,tc,tcName,section);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(meaningsPanel);
	}

	public void setUpMeanings(Vector<Meaning> mngs, TermClass tc, String tcName, String section){
		termClass = tc;
		meaningsPanel.removeAll();
		rowCounter = 0;
		gbc.gridy = 0;
		gbc.gridx = 0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.weighty = 1;
		gbc.weightx = 1;
		Meaning m;
		for(int i = 0;i<mngs.size();i++){
			m = mngs.elementAt(i);
			gbc.weightx = 0;
			gbc.gridwidth = 5;
			JLabel wordLabel = new JLabel(((mngs.size()>1)?(++rowCounter)+". ":"")+m.getMeaningWithArticle());
			wordLabel.setOpaque(false);
			wordLabel.setFont(meaningFont);
			JPanel wordPanel = new JPanel();
			wordPanel.setOpaque(false);
			wordPanel.setLayout(new GridBagLayout());
			wlgbc.gridy = 0;
			wordPanel.add(wordLabel,wlgbc);
			if(m.hasSynonyms() && wcSettings.getShowSynonyms()){
				JLabel synonymsLabel = new JLabel(m.getSynonymsAsString());
				synonymsLabel.setOpaque(false);
				wlgbc.gridy = 1;
				wordPanel.add(synonymsLabel,wlgbc);
			}
			
			JPanel infoPanel = new JPanel();
			infoPanel.setOpaque(false);
			infoPanel.setLayout(new GridBagLayout());
			if(m.hasCategory()){
				++gbc.gridy;
				JLabel catLabel = new JLabel(LANG.getCategory()+": "+m.getCategory());
				catLabel.setOpaque(false);
				catLabel.setVerticalAlignment(SwingConstants.TOP);
				infoPanel.add(catLabel,gbc);
			}
			if(m.hasPhonetic() && wcSettings.getShowPhonetics()){
				++gbc.gridy;
				JLabel phoneticLabel = new JLabel(m.getPhonetic());
				phoneticLabel.setOpaque(false);
				phoneticLabel.setVerticalAlignment(SwingConstants.TOP);
				phoneticLabel.setFont(phoneticFont);
				infoPanel.add(phoneticLabel,gbc);
			}
			gbc.gridwidth = 1;
			if(m.hasMeta() && wcSettings.getTestMetas()){
				++gbc.gridy;
				JPanel metaPanel = new JPanel();
				metaPanel.setOpaque(false);
				metaPanel.setLayout(new GridBagLayout());
				mgbc.gridy =0;
				mgbc.gridwidth =1;
				for(String key : termClass.getSortedKeys()){
					if(m.hasMetaEntryFor(key)){
						mgbc.gridy++;
						JLabel keyLabel = new JLabel(key+":");
						keyLabel.setOpaque(false);
						keyLabel.setHorizontalAlignment(SwingConstants.LEFT);
						keyLabel.setFont(keyFont);
						JLabel valLabel = new JLabel(m.getMetaElement(key));
						valLabel.setOpaque(false);
						valLabel.setFont(valFont);
						mgbc.gridx = 1;
						mgbc.insets = spaceLeftTenInsets;
						metaPanel.add(keyLabel,mgbc);
						mgbc.gridx = 2;
						metaPanel.add(valLabel,mgbc);
					}
				}
				mgbc.weighty =1;
				++mgbc.gridy;
				mgbc.gridwidth =2;
				JPanel filler = new JPanel();
				filler.setOpaque(false);
				metaPanel.add(filler,mgbc);
				infoPanel.add(metaPanel,gbc);
			}
			if(m.hasNote() && wcSettings.getShowNote()){
				CustomTextArea noteTA = new CustomTextArea(m.getNote(), false, false);
				noteTA.setOpaque(false);
				JScrollPane noteSP = new JScrollPane(noteTA);
				noteSP.setPreferredSize(new Dimension(130,60));
				LabelPanel noteLP = new LabelPanel(LANG.getNote()+":",noteSP);
				noteLP.setOpaque(false);
				if(!m.hasMeta())++gbc.gridy;
				++gbc.gridx;
				gbc.insets = spaceLeftFiveInsets;
				infoPanel.add(noteLP,gbc);
			}
			if(m.hasExampleSentence() && wcSettings.getShowESentence()){
				CustomTextArea esTA = new CustomTextArea(m.getExampleSentence(), false, false);
				esTA.setOpaque(false);
				CustomScrollPane jsp = new CustomScrollPane(esTA);
				jsp.setPreferredSize(new Dimension(130,60));
				LabelPanel lp = new LabelPanel(LANG.getExampleSentence()+":",jsp);
				lp.setOpaque(false);
				++gbc.gridx;
				gbc.insets = spaceLeftFiveInsets;
				infoPanel.add(lp,gbc);
			}
			gbc.weightx = 1;
			++gbc.gridx;
			JPanel filler = new JPanel();
			filler.setOpaque(false);
			infoPanel.add(filler,gbc);
			gbc.gridheight = 1;
			gbc.gridx = 0;
			gbc.gridy++;
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.insets = noInsets;
			meaningsPanel.add(wordPanel,gbc);
			gbc.gridy++;
			gbc.weightx = 0;
			meaningsPanel.add(infoPanel,gbc);
		}
	}
}